add cannot_create_events! annotation

Andrew Cantino 11 years ago
parent
commit
0150eac1ac

+ 21 - 1
app/models/agent.rb

@@ -88,7 +88,11 @@ class Agent < ActiveRecord::Base
88 88
   end
89 89
 
90 90
   def create_event(attrs)
91
-    events.create!({ :user => user }.merge(attrs))
91
+    if can_create_events?
92
+      events.create!({ :user => user }.merge(attrs))
93
+    else
94
+      error "This Agent cannot create events!"
95
+    end
92 96
   end
93 97
 
94 98
   def validate_schedule
@@ -140,6 +144,14 @@ class Agent < ActiveRecord::Base
140 144
     !cannot_receive_events?
141 145
   end
142 146
 
147
+  def cannot_create_events?
148
+    self.class.cannot_create_events?
149
+  end
150
+
151
+  def can_create_events?
152
+    !cannot_create_events?
153
+  end
154
+
143 155
   def set_last_checked_event_id
144 156
     if newest_event_id = Event.order("id desc").limit(1).pluck(:id).first
145 157
       self.last_checked_event_id = newest_event_id
@@ -169,6 +181,14 @@ class Agent < ActiveRecord::Base
169 181
       @default_schedule
170 182
     end
171 183
 
184
+    def cannot_create_events!
185
+      @cannot_create_events = true
186
+    end
187
+
188
+    def cannot_create_events?
189
+      !!@cannot_create_events
190
+    end
191
+
172 192
     def cannot_receive_events!
173 193
       @cannot_receive_events = true
174 194
     end

+ 2 - 0
app/models/agents/digest_email_agent.rb

@@ -3,6 +3,8 @@ module Agents
3 3
     MAIN_KEYS = %w[title message text main value].map(&:to_sym)
4 4
     default_schedule "5am"
5 5
 
6
+    cannot_create_events!
7
+
6 8
     description <<-MD
7 9
       The DigestEmailAgent collects any Events sent to it and sends them all via email when run.
8 10
       The email will be sent to your account's address and will have a `subject` and an optional `headline` before

+ 1 - 0
app/models/agents/post_agent.rb

@@ -1,6 +1,7 @@
1 1
 module Agents
2 2
   class PostAgent < Agent
3 3
     cannot_be_scheduled!
4
+    cannot_create_events!
4 5
 
5 6
     description <<-MD
6 7
        Post Agent receives events from other agents and send those events as the contents of a post request to a specified url. `post_url` field must specify where you would like to receive post requests and do not forget to include URI scheme (`http` or `https`)

+ 1 - 0
app/models/agents/twilio_agent.rb

@@ -4,6 +4,7 @@ require 'securerandom'
4 4
 module Agents
5 5
   class TwilioAgent < Agent
6 6
     cannot_be_scheduled!
7
+    cannot_create_events!
7 8
 
8 9
     description <<-MD
9 10
       The TwilioAgent receives and collects events and sends them via text message or gives you a call when scheduled.

+ 2 - 1
app/views/agents/_form.html.erb

@@ -47,8 +47,9 @@
47 47
   <div class="control-group">
48 48
     <%= f.label :sources, :class => 'control-label' %>
49 49
     <div class="controls link-region" data-can-receive-events="<%= @agent.can_receive_events? %>">
50
+      <% eventSources = (current_user.agents - [@agent]).find_all { |a| a.can_create_events? } %>
50 51
       <%= f.select(:source_ids,
51
-                   options_for_select((current_user.agents - [@agent]).map {|s| [s.name, s.id] },
52
+                   options_for_select(eventSources.map {|s| [s.name, s.id] },
52 53
                                       @agent.source_ids),
53 54
                    {}, { :multiple => true, :size => 5, :class => 'span4 select2' }) %>
54 55
       <span class='cannot-receive-events text-info'>This type of Agent cannot receive events.</span>

+ 15 - 5
spec/models/agent_spec.rb

@@ -114,13 +114,23 @@ describe Agent do
114 114
     end
115 115
 
116 116
     describe "#create_event" do
117
+      before do
118
+        @checker = Agents::SomethingSource.new(:name => "something")
119
+        @checker.user = users(:bob)
120
+        @checker.save!
121
+      end
122
+
117 123
       it "should use the checker's user" do
118
-        checker = Agents::SomethingSource.new(:name => "something")
119
-        checker.user = users(:bob)
120
-        checker.save!
124
+        @checker.check
125
+        Event.last.user.should == @checker.user
126
+      end
121 127
 
122
-        checker.check
123
-        Event.last.user.should == checker.user
128
+      it "should log an error if the Agent has been marked with 'cannot_create_events!'" do
129
+        mock(@checker).can_create_events? { false }
130
+        lambda {
131
+          @checker.check
132
+        }.should_not change { Event.count }
133
+        @checker.logs.first.message.should =~ /cannot create events/i
124 134
       end
125 135
     end
126 136